﻿using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace AR.Models
{
    public static class UploadCommands
    {
        public static void UploadFile(string cvFileName, Stream data)
        {
            UriBuilder ub = new UriBuilder(System.Windows.Application.Current.Host.Source.AbsoluteUri.Replace("/ClientBin/AR.xap", "/receiver.ashx"));
            ub.Query = string.Format("filename={0}", cvFileName);
            WebClient c = new WebClient();
            c.OpenWriteCompleted += (sender, e) =>
            {
                PushData(data, e.Result);
                e.Result.Close();
                data.Close();

            };
            c.OpenWriteAsync(ub.Uri);

        }

        public static void PushData(Stream input, Stream output)
        {
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)
            {
                output.Write(buffer, 0, bytesRead);
            }
        }

    }
}
